package com.mcxx.modules.minimumliving.service.impl;

import com.mcxx.modules.minimumliving.service.SalaryRecoveryService;
import com.mcxx.modules.base.utils.QueryParamUtil;
import com.mcxx.modules.filemanage.easypoi.util.ExportExcelService;
import com.mcxx.modules.filemanage.file.service.FileService;
import com.mcxx.modules.miniSalvation.family.dto.SalvationFamilyDTO;
import com.mcxx.modules.miniSalvation.fundpay.dto.FundRecoverDTO;
import com.mcxx.modules.miniSalvation.fundpay.dto.FundRecoverExcelDTO;
import com.mcxx.modules.miniSalvation.fundpay.entity.FundRecoverEntity;
import com.mcxx.modules.miniSalvation.fundpay.service.read.FundRecoverReadService;
import com.mcxx.modules.miniSalvation.fundpay.service.write.FundRecoverWriteService;
import com.mcxx.modules.miniSalvation.salary.param.SalvationRecoveryParam;
import com.mcxx.modules.system.entity.TaskExportEntity;
import com.mcxx.modules.system.service.write.TaskExportWriteService;
import com.mcxx.util.CommonUtil;
import com.yinhai.ta404.component.audit.vo.ExcelInfo;
import com.yinhai.ta404.core.restservice.requestbean.PageParam;
import com.yinhai.ta404.core.restservice.resultbean.Page;
import com.yinhai.ta404.core.security.vo.UserAccountVo;
import com.yinhai.ta404.core.service.BaseService;
import com.yinhai.ta404.core.transaction.annotation.TaTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service("salaryRecoveryServiceImpl")
@TaTransactional
public class SalaryRecoveryServiceImpl extends BaseService implements SalaryRecoveryService {
    @Autowired
    private FundRecoverReadService fundRecoverReadService;
    @Autowired
    private FundRecoverWriteService fundRecoverWriteService;
    @Autowired
    private ExportExcelService exportExcelService;
    @Autowired
    private FileService fileService;
    @Autowired
    private TaskExportWriteService taskExportWriteService;

    @Override
    public Page<SalvationFamilyDTO> getSalvationRecoveryFamilyList(SalvationRecoveryParam param, PageParam pageParam,
                                                                   UserAccountVo accountVo) {

        QueryParamUtil.setBaseParamValue(param, accountVo);

        return fundRecoverReadService.getSalvationRecoveryFamilyList(param, pageParam);
    }

    @Override
    public Page<FundRecoverDTO> getRecoveryList(SalvationRecoveryParam param, PageParam pageParam,
                                                UserAccountVo accountVo) {

        QueryParamUtil.setBaseParamValue(param, accountVo);

        Page<FundRecoverDTO> page = fundRecoverReadService.getRecoveryList(param, pageParam);
        return page;
    }

    @Override
    public void addSalaryRecovery(FundRecoverEntity param,UserAccountVo userAccountVo) {

        if(CommonUtil.invalidArgs(param.getOperator())){
            param.setOperator(userAccountVo.getUsername());
        }

        fundRecoverReadService.verifyMoney(param);

        fundRecoverReadService.verifyRecover(param);


        fundRecoverWriteService.addSalaryRecovery(param);
    }

    @Override
    public int modifySalaryRecovery(FundRecoverEntity param) {

        fundRecoverReadService.verifyMoney(param);

        return fundRecoverWriteService.modifySalaryRecovery(param);
    }

    @Override
    public int cancelSalaryRecovery(String ids) {
        return fundRecoverWriteService.cancelSalaryRecovery(ids);
    }

    @Override
    public int confirmSalaryRecovery(String ids) {
        return fundRecoverWriteService.confirmSalaryRecovery(ids);
    }

    @Override
    public int delSalaryRecovery(String ids) {
        return fundRecoverWriteService.delSalaryRecovery(ids);
    }

    
    @Override
    public ExcelInfo exportExcelSave(SalvationRecoveryParam param, UserAccountVo accountVo) {
        ExcelInfo excelInfo = null;
        QueryParamUtil.setBaseParamValue(param,accountVo);
        List<FundRecoverExcelDTO> list = fundRecoverReadService.getRecoveryListExcel(param);
        excelInfo = exportExcelService.exportOrgOpLogs(FundRecoverExcelDTO.class,list,"追回救助金信息.xls",
                "追回救助金信息列表","追回救助金信息列表",false,"");
        
        return excelInfo;
    }
    
    public int canConfirmSalaryRecovery(String id){
        FundRecoverEntity entity = fundRecoverReadService.getById(id);
        return entity.getState();
    }
    
    public FundRecoverDTO queryRecoveryInfo(String id){
        FundRecoverDTO dto = fundRecoverReadService.selectRecoverInfo(id);
        return dto;
    }
}
